//
//  UITableView+Extend.m
//  RMMaterialRed
//
//  Created by XBWH on 2024/3/7.
//

#import "UITableView+Extend.h"
#import <objc/runtime.h>

static char UITableViewEmptyView;

@implementation UITableView (Extend)
-(UIView *)emptyView{
    return objc_getAssociatedObject(self, &UITableViewEmptyView);
}
- (void)setEmptyView:(UIView *)emptyView{
    [self willChangeValueForKey:@"HJEmptyView"];
    objc_setAssociatedObject(self, &UITableViewEmptyView,
                             emptyView,
                             OBJC_ASSOCIATION_ASSIGN);
    [self didChangeValueForKey:@"HJEmptyView"];
}
- (void)rb_tableViewDisplayWithMsg:(NSString *)message blankImg:(NSString *)blankImg necessaryForRowCount:(NSUInteger)rowCount type:(NSInteger)type{
    if (rowCount == 0) {
        self.mj_footer.hidden  = YES;
        // 没有数据的时候，UILabel的显示样式
        self.backgroundView = [[UIView alloc]initWithFrame:self.bounds];
        self.backgroundView.backgroundColor =self.backgroundColor;
        UIImageView * blankImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:blankImg]];
        [self.backgroundView addSubview:blankImgView];
        [blankImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            if (type==1) {
                make.top.equalTo(self.backgroundView).mas_offset(88);
            }else if (type==2){
                make.bottom.equalTo(self.backgroundView.mas_centerY).mas_offset(-30);
            }else if (type==3){
                 make.bottom.equalTo(self.backgroundView.mas_centerY).mas_offset(100);
            }else if (type ==4){
                make.bottom.equalTo(self.backgroundView.mas_centerY).mas_offset(200);
            }else if (type ==5){
                make.bottom.equalTo(self.backgroundView.mas_centerY).mas_offset(350);
            }
            make.centerX.equalTo(self.backgroundView);
            make.size.mas_equalTo(CGSizeMake(100, 100));
        }];
        UILabel *messageLabel = [UILabel new];
        messageLabel.text = message;
        messageLabel.font = RBPingFangFont(16);
        messageLabel.textColor = RBColorFromRGB(0x999999);
        messageLabel.textAlignment = NSTextAlignmentCenter;
        [messageLabel sizeToFit];
        [self.backgroundView addSubview:messageLabel];
        [messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(blankImgView.mas_bottom).offset(10);
            make.centerX.equalTo(self.backgroundView);
        }];
    } else {
        self.mj_footer.hidden  = NO;
        self.backgroundView = nil;
    }
}
@end
